package com.linkage.educloud.ah.bean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mr'Tang on 2018/12/13.
 */

public class PersonInfo {

    private long id;
    private String name;
    private String tel;
    private String avater;
    private String gradename;
    private int gradeid;
    private List<ClassInfo> classlist;
    private List<ItemInfo> gradelist;
    private List<ItemInfo> usercall;
    private List<ItemInfo> subjects;

    public static class ClassInfo {
        private int role;
        private String callname;
        private String subject;
        private String school;
        private String classname;
        private String classid;
        private String gradename;
        private String childrenid;
        private String childrenname;

        public int getRole() {
            return role;
        }

        public void setRole(int role) {
            this.role = role;
        }

        public String getCallname() {
            return callname;
        }

        public void setCallname(String callname) {
            this.callname = callname;
        }

        public String getSubject() {
            return subject;
        }

        public void setSubject(String subject) {
            this.subject = subject;
        }

        public String getSchool() {
            return school;
        }

        public void setSchool(String school) {
            this.school = school;
        }

        public String getClassname() {
            return classname;
        }

        public void setClassname(String classname) {
            this.classname = classname;
        }

        public String getClassid() {
            return classid;
        }

        public void setClassid(String classid) {
            this.classid = classid;
        }

        public String getGradename() {
            return gradename;
        }

        public void setGradename(String gradename) {
            this.gradename = gradename;
        }

        public String getChildrenid() {
            return childrenid;
        }

        public void setChildrenid(String childrenid) {
            this.childrenid = childrenid;
        }

        public String getChildrenname() {
            return childrenname;
        }

        public void setChildrenname(String childrenname) {
            this.childrenname = childrenname;
        }
    }

    public static class ItemInfo implements Serializable {
        private int id;
        private String name;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public static List<ItemInfo> fromJsonObject(JSONObject json) {
            List<ItemInfo> infoList = new ArrayList<ItemInfo>();
            if (json.has("list") && !json.isNull("list")) {
                JSONArray jas;
                try {
                    jas = json.getJSONArray("list");
                    for (int i = 0; i < jas.length(); i++) {
                        JSONObject obj = jas.getJSONObject(i);
                        ItemInfo info = new ItemInfo();
                        info.id = obj.optInt("id");
                        info.name = obj.getString("name");
                        infoList.add(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return infoList;
        }

    }

    public String getGradename() {
        return gradename;
    }

    public void setGradename(String gradename) {
        this.gradename = gradename;
    }

    public int getGradeid() {
        return gradeid;
    }

    public void setGradeid(int gradeid) {
        this.gradeid = gradeid;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getAvater() {
        return avater;
    }

    public void setAvater(String avater) {
        this.avater = avater;
    }

    public List<ClassInfo> getClasslist() {
        return classlist;
    }

    public void setClasslist(List<ClassInfo> classlist) {
        this.classlist = classlist;
    }

    public List<ItemInfo> getGradelist() {
        return gradelist;
    }

    public void setGradelist(List<ItemInfo> gradelist) {
        this.gradelist = gradelist;
    }

    public List<ItemInfo> getUsercall() {
        return usercall;
    }

    public void setUsercall(List<ItemInfo> usercall) {
        this.usercall = usercall;
    }

    public List<ItemInfo> getSubjects() {
        return subjects;
    }

    public void setSubjects(List<ItemInfo> subjects) {
        this.subjects = subjects;
    }

    public static PersonInfo getFromJSON(JSONObject json) {
        PersonInfo per = new PersonInfo();
        try {
            per.name = json.getString("username");
            per.tel = json.getString("tel");
            if (json.has("gradelist") && !json.isNull("gradelist")) {
                List<ItemInfo> gradelist = new ArrayList<PersonInfo.ItemInfo>();
                JSONArray gradejsa = json.getJSONArray("gradelist");
                for (int j = 0; j < gradejsa.length(); j++) {
                    JSONObject gradeobj = gradejsa.getJSONObject(j);
                    ItemInfo item = new ItemInfo();
                    item.id = gradeobj.optInt("gradeid");
                    item.name = gradeobj.optString("gradename");
                    gradelist.add(item);
                }
                per.gradelist = gradelist;
            }
            if (json.has("usercall") && !json.isNull("usercall")) {
                List<ItemInfo> calllist = new ArrayList<PersonInfo.ItemInfo>();
                JSONArray gradejsa = json.getJSONArray("usercall");
                for (int j = 0; j < gradejsa.length(); j++) {
                    JSONObject gradeobj = gradejsa.getJSONObject(j);
                    ItemInfo item = new ItemInfo();
                    item.id = gradeobj.optInt("callid");
                    item.name = gradeobj.optString("called");
                    calllist.add(item);
                }
                per.usercall = calllist;
            }
            if (json.has("subjects") && !json.isNull("subjects")) {
                List<ItemInfo> subjects = new ArrayList<PersonInfo.ItemInfo>();
                JSONArray gradejsa = json.getJSONArray("subjects");
                for (int j = 0; j < gradejsa.length(); j++) {
                    JSONObject gradeobj = gradejsa.getJSONObject(j);
                    ItemInfo item = new ItemInfo();
                    item.id = gradeobj.optInt("subjectid");
                    item.name = gradeobj.optString("subject");
                    subjects.add(item);
                }
                per.subjects = subjects;
            }
            List<ClassInfo> classlist = new ArrayList<PersonInfo.ClassInfo>();
            if (json.has("classinfo") && !json.isNull("classinfo")) {
                JSONArray jsa = json.getJSONArray("classinfo");
                for (int i = 0; i < jsa.length(); i++) {
                    JSONObject obj = jsa.getJSONObject(i);
                    ClassInfo info = new ClassInfo();
                    info.classid = obj.optString("classid");
                    info.school = obj.optString("school");
                    info.classname = obj.optString("classname");
                    info.role = obj.optInt("role");
                    info.childrenname = obj.optString("childrenname");
                    info.childrenid = obj.optString("childrenid");
                    for (ItemInfo itemInfo : per.getSubjects()) {
                        if (obj.optString("subject").equals(itemInfo.getId())) {
                            info.subject = itemInfo.getName();
                        }
                    }
                    for (ItemInfo itemInfo : per.getGradelist()) {
                        if (obj.optString("gradename").equals(itemInfo.getId())) {
                            info.gradename = itemInfo.getName();
                        }
                    }
                    for (ItemInfo itemInfo : per.getUsercall()) {
                        if (obj.optString("callname").equals(itemInfo.getId())) {
                            info.callname = itemInfo.getName();
                        }
                    }
                    classlist.add(info);
                }
            }
            per.classlist = classlist;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return per;
    }
}
